package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.ISsgdServiceImpl;
import com.augurit.gzcgwsp.ssgd.service.IYwRqSsgdSqbService;
import com.augurit.gzcgwsp.ssgd.web.form.YwRqSsgdSqbForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 解析测试改动xml数据
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class SsgdServiceImpl implements ISsgdServiceImpl {
	Logger logger = (Logger) LoggerFactory.getLogger(SsgdServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;
	@Autowired
	private IYwRqSsgdSqbService ywRqSsgdSqbService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// 测试改动的xml存放路径
	private final String ssgdfilepath = localResource.getString("ssgdfilepath");
	// 测试改动的附件存放路径
	private final String ssgdfujianpath = localResource.getString("ssgdfujianpath");
	// 测试改动的解析成功反馈xml存放路径
	private final String ssgdfeedbackPath = localResource.getString("ssgdfeedbackPath");
	// 测试改动的异常xml存放路径
	private final String ssgdexceptionPath = localResource.getString("ssgdexceptionPath");
	// 备份
	private final String ssgdbackPath = localResource.getString("ssgdbackPath");
	private Map<String, String> ssgdKeyMap;// xml内容的key
	private Map<String, String> ssgdKeyMapHead;// xml头部的key
	private Map<String, Long> ssgdfujianMap;// 附件的别名和id对应的map
	private String errorCode = "";// 异常编号
	private String errorStatus = "";// 异常描述
	private String errorStack = "";// 异常堆栈

	private void initSsgdMap() {
		// 初始化数据部分对应的map
		ssgdKeyMap = new HashMap<String, String>();
		ssgdKeyMap.put("YSL_NO", "YSL_NO");
		ssgdKeyMap.put("approvalNo", "approvalNo");
		ssgdKeyMap.put("businessCode", "businessCode");
		ssgdKeyMap.put("sendTime", "sendTime");
		
		ssgdKeyMap.put("SQDW", "SQDW");
		ssgdKeyMap.put("SQRQ", "SQRQ");
		ssgdKeyMap.put("LXR", "LXR");
		ssgdKeyMap.put("LXR_TEL", "LXR_TEL");
		ssgdKeyMap.put("LXR_SJ", "LXR_SJ");
		ssgdKeyMap.put("SSGDMC", "SSGDMC");
		ssgdKeyMap.put("SSGDDD", "SSGDDD");
		ssgdKeyMap.put("SSXZQY", "SSXZQY");
		ssgdKeyMap.put("GDZYNR", "GDZYNR");
		ssgdKeyMap.put("KNSYXQY", "KNSYXQY");
		ssgdKeyMap.put("KNSYXYH", "KESYXYH");
		ssgdKeyMap.put("JHSSSJ", "JHSSSJ");
		ssgdKeyMap.put("JHWCSJ", "JHWCSJ");
		ssgdKeyMap.put("GDLX", "GDLX");
		ssgdKeyMap.put("SSGDYY", "SHYJ");
		

		// 初始化头部对应的map
		ssgdKeyMapHead = new HashMap<String, String>();
		ssgdKeyMapHead.put("sn", "sn");
		ssgdKeyMapHead.put("sender", "sender");
		ssgdKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		ssgdfujianMap = new HashMap<String, Long>();
		ssgdfujianMap.put("FJ_01", 41l);
		ssgdfujianMap.put("FJ_02", 43l);
		ssgdfujianMap.put("FJ_03", 42l);
		ssgdfujianMap.put("FJ_04", 44l);
		ssgdfujianMap.put("FJ_05", 45l);
		ssgdfujianMap.put("FJ_06", 46l);
		ssgdfujianMap.put("FJ_07", 181l);
	}

	public void readXml(String projectPath) {
		if (ssgdKeyMap == null)
			this.initSsgdMap();

		File file = new File(ssgdfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length>0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(ssgdKeyMap, filePath,ssgdfeedbackPath, ssgdexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(ssgdKeyMapHead,files[i], ssgdfeedbackPath, ssgdexceptionPath);

						// 把数据部分保存到数据库
						YwRqSsgdSqbForm form = new YwRqSsgdSqbForm();
						for (String mapKey : ssgdKeyMap.keySet()) {

							// TODO 把值放到form中
							form.setYslNo(parseXMLService.getMapValue(ssgdKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(ssgdKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(ssgdKeyMap.get("SQDW"),"rqssgd");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							String sqrqTemp = parseXMLService.getMapValue(ssgdKeyMap.get("SQRQ"));
							Date sqrq = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(sqrqTemp)) {
								sqrq = DateUtils.stringToDate(sqrqTemp,DateUtils.YYYY_MM_DD);
								form.setSqrq(sqrq);
							}else{
								form.setSqrq(new Date());
							}

							form.setYwlxr(parseXMLService.getMapValue(ssgdKeyMap.get("LXR")));
							form.setYwlxrtel(parseXMLService.getMapValue(ssgdKeyMap.get("LXR_TEL")));
							form.setYwlxphone(parseXMLService.getMapValue(ssgdKeyMap.get("LXR_SJ")));
							form.setSsgdmc(parseXMLService.getMapValue(ssgdKeyMap.get("SSGDMC")));
							form.setAddress(parseXMLService.getMapValue(ssgdKeyMap.get("SSGDDD")));
							form.setSsxzqy(parseXMLService.getMapValue(ssgdKeyMap.get("SSXZQY")));
							form.setGdzynr(parseXMLService.getMapValue(ssgdKeyMap.get("GDZYNR")));
							form.setKnsyxqy(parseXMLService.getMapValue(ssgdKeyMap.get("KNSYXQY")));
							form.setKnsyxyh(parseXMLService.getMapValue("KNSYXYH"));
							String JHSSSJTemp = parseXMLService.getMapValue(ssgdKeyMap.get("JHSSSJ"));
							Date JHSSSJ = null;
							if (StringUtils.isNotEmpty(JHSSSJTemp)) {
								JHSSSJ = DateUtils.stringToDate(JHSSSJTemp,DateUtils.YYYY_MM_DD);
								form.setJhsssj(JHSSSJ);
							}

							String JHWCSJTemp = parseXMLService.getMapValue(ssgdKeyMap.get("JHWCSJ"));
							Date JHWCSJ = null;
							if (StringUtils.isNotEmpty(JHWCSJTemp)) {
								JHWCSJ = DateUtils.stringToDate(JHWCSJTemp,DateUtils.YYYY_MM_DD);
								form.setJhwcsj(JHWCSJ);
							}

							form.setGdlx(parseXMLService.getMapValue(ssgdKeyMap.get("GDLX")));
							form.setSsgdyy(parseXMLService.getMapValue("SSGDYY"));							
							form.setSarq(new Date());

							form.setApprovalNo(parseXMLService.getMapValue(ssgdKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(ssgdKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(ssgdKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setBranch("rqssgd");
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywRqSsgdSqbService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									ssgdKeyMapHead, ssgdfeedbackPath, ssgdexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, ssgdfujianpath, ssgdfeedbackPath, ssgdexceptionPath, ssgdKeyMapHead,
								files, ssgdfujianMap, i, form.getQyid(), form.getId(), "rqssgd");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								ssgdKeyMapHead, ssgdfeedbackPath, ssgdexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, ssgdbackPath + fileName,fileName);
				}
			}
		}
	}
}
